<template>
  <div class="wrapper">
    <div class="content-wrapper">
      <div class="orderHeader" style="margin: 5px 0px 0px 0px">
        <div class="orderSearch">
          <el-form ref="form" :model="searchParams" :inline="true">
            <el-form-item label="培训名称">
              <el-input
                v-model="searchParams.name"
                clearable
                placeholder="请填写培训名称"
              />
            </el-form-item>
            <el-form-item label="培训时间">
              <el-date-picker
                @change="handleTimeChange"
                v-model="timeRange"
                type="datetimerange"
                :picker-options="pickerOptions"
                value-format="timestamp"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button
                id="go_search"
                type="primary"
                icon="el-icon-search"
                data-tracker="P10-3"
                @click="handleSearch"
                >查询
              </el-button>
              <el-button
                type="primary"
                plain
                icon="el-icon-refresh"
                data-tracker="P10-4"
                @click="handleReset"
                >重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="actions" style="margin-bottom: 10px">
        <div class="left">
          <el-button type="primary" plain size="mini" @click="handleClickAdd"
            >新增</el-button
          >
          <el-button type="primary" plain size="mini" @click="handleClickDel"
            >删除</el-button
          >
          <el-button type="primary" plain size="mini" @click="handleClickExport"
            >导出</el-button
          >
        </div>
        <div class="right">
          <el-pagination
            background
            small
            :current-page="paginations.pageIndex"
            :page-sizes="paginations.pageSizes"
            :page-size="paginations.pageSize"
            :layout="paginations.layout"
            :total="paginations.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        stripe
        align="center"
        @selection-change="handleSelectionChange"
        :max-height="maxheight"
      >
        <el-table-column type="selection" width="45"> </el-table-column>
        <el-table-column
          prop=""
          label="序号"
          width="50"
          type="index"
        ></el-table-column>
        <el-table-column prop="name" label="培训名称" width="180">
        </el-table-column>
        <el-table-column
          prop="plan_training_lecturer_name"
          label="培训讲师"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="plan_training_time" label="培训时间" width="180">
          <template slot-scope="scope">
            <span v-time="scope.row.plan_training_time"></span>
          </template>
        </el-table-column>

        <el-table-column prop="training_place" label="培训地点">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag
              size="mini"
              effect="dark"
              :type="['', '', 'success', 'warning', 'danger'][scope.row.status]"
            >
              {{ ["", "草稿", "未培训", "已培训", "作废"][scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="formwork_list" label="模板">
                <template slot-scope="scope">
                    <div v-for="(item, index) in scope.row.formwork_list" :key="index" class="text item">
                        <el-link :href="item.file_url" target="_blank">{{ item.file_name }}</el-link>
                    </div>
                </template>
            </el-table-column> -->
        <el-table-column
          prop=""
          fixed="right"
          align="center"
          label="操作"
          width="160"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              plain
              size="mini"
              @click="handleOperation(scope.row, 'detail')"
              >详情
            </el-button>
            <el-button
              type="text"
              plain
              size="mini"
              @click="handleOperation(scope.row, 'edit')"
              >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  trainingPlanList,
  trainingPlanDelete,
  trainingPlanListExport,
} from "@/api/qms.js";
import { timestamp10 } from "@/utils/mUtils";
export default {
  name: "refundReviewList",
  components: {},
  props: [],
  data() {
    return {
      maxheight: "auto",
      tableData: [],
      paginations: {
        pageIndex: 1, // 当前位于哪页
        pageSize: 20, // 1页显示多少条
        pageSizes: [10, 20, 50, 100], //每页显示多少条
        total: 200,
        layout: "total, sizes, prev, pager, next, jumper", // 翻页属性
      },
      checkedList: [],
      searchParams: {},
      mode: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      timeRange: [],
    };
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    handleTimeChange(val) {
      if (val) {
        this.searchParams.plan_training_start_time = timestamp10(val[0]) * 1;
        this.searchParams.plan_training_end_time = timestamp10(val[1]) * 1;
      } else {
        this.searchParams.plan_training_start_time = undefined;
        this.searchParams.plan_training_end_time = undefined;
      }
    },
    handleSearch() {
      this.paginations.pageIndex = 1;
      this.getDataList();
    },
    handleReset() {
      this.paginations.pageIndex = 1;
      this.searchParams = {};
      this.timeRange = [];
      this.getDataList();
    },
    handleClickAdd() {
      this.mode = "add";
      this.$router.push({
        name: "train",
      });
    },
    handleClickDel() {
      trainingPlanDelete({
        ids: this.checkedList,
      }).then((res) => {
        if (res.code === 10000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getDataList();
        }
      });
    },
    handleClickExport() {
      trainingPlanListExport({
        page: this.paginations.pageIndex,
        page_size: this.paginations.pageSize,
        ...this.searchParams,
      }).then((res) => {
        this.reqUrl = "https://" + res.data.data;
        window.open(this.reqUrl);
      });
    },
    handleOperation(row, mode) {
      this.$router.push({
        name: "train",
        query: {
          id: row.id,
          mode,
        },
      });
    },
    handleSelectionChange(checked) {
      this.checkedList = checked.map((item) => item.id);
    },
    handleCloseDialog() {
      this.dialogVisible = false;
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.paginations.pageSize = val;
      this.getDataList();
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.paginations.pageIndex = val;
      this.getDataList();
    },
    getDataList() {
      trainingPlanList({
        page: this.paginations.pageIndex,
        page_size: this.paginations.pageSize,
        ...this.searchParams,
      }).then((res) => {
        this.tableData = res.data.list || [];
        if (this.tableData.length > 6) {
          //两排操作栏
          this.maxheight = document.documentElement.clientHeight - 265;
        } else {
          this.maxheight = "auto";
        }

        this.paginations.total = res.data.pagination.total;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  padding: 10px 15px;
  background-color: #fff;

  .content-wrapper {
    // padding: 10px;
  }

  .orderHeader {
    margin-top: 5px;
    background-color: #fff;

    .orderSearch {
      form {
        ::v-deep .el-form-item__label {
          font-size: 14px;
        }

        .el-textarea {
          width: 135px;
        }
      }
    }
  }

  .actions {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    width: 100%;
    background-color: #fff;

    .el-button + .el-dropdown,
    .el-dropdown + .el-button {
      margin-left: 10px;
    }
  }
}
</style>